import type { RouteLocationNormalized } from 'vue-router';
import { defineStore } from 'pinia';
import { DEFAULT_ROUTE_NAME, REDIRECT_ROUTE_NAME } from '@/router/constants';
import { isString } from '@/utils/is';
import defaultSettings from '@/config/settings.json';
import { TabBarState, TagProps } from './types';

const formatTag = (
  route: RouteLocationNormalized,
  portal: string
): TagProps => {
  const { name, meta, fullPath, query } = route;
  return {
    title: meta.locale || '',
    label: meta.label,
    name: String(name),
    fullPath,
    query,
    ignoreCache: meta.ignoreCache,
    portal,
  };
};

const BAN_LIST = [REDIRECT_ROUTE_NAME];

const useTabBarStore = defineStore('tabBar', {
  state: (): TabBarState => ({
    cacheTabList: new Set([]),
    tagList: [
      {
        fullPath: '/dashboard/workplace',
        ignoreCache: undefined,
        label: undefined,
        name: 'Workplace',
        portal: '/system',
        query: {},
        title: 'menu.dashboard.workplace',
      },
    ],
    portalMap: new Map<string, TagProps[]>(),
    crossPortalTab: defaultSettings.crossPortalTab,
    tabBarOpenMax: defaultSettings.tabBarOpenMax,
  }),

  getters: {
    getTabList(): TagProps[] {
      return this.tagList;
    },
    getCacheList(): string[] {
      return Array.from(this.cacheTabList);
    },
  },

  actions: {
    updateTabList(route: RouteLocationNormalized, portal: string) {
      if (BAN_LIST.includes(route.name as string)) return;

      if (this.tagList.length === this.tabBarOpenMax) {
        this.deleteTag(1, this.tagList[1]);
      }
      this.tagList.push(formatTag(route, portal));

      if (!route.meta.ignoreCache) {
        this.cacheTabList.add(route.name as string);
      }
    },
    togglePortalTabList(fromPortal: string, toPortal: string) {
      if (this.crossPortalTab) {
        if (fromPortal) {
          this.portalMap.set(fromPortal, this.tagList);
        }
        this.tagList = this.portalMap.get(toPortal) || [];
      }
    },
    deleteTag(idx: number, tag: TagProps) {
      this.tagList.splice(idx, 1);
      this.cacheTabList.delete(tag.name);
    },
    deleteTagByFullPath(fullPath: string) {
      this.tagList.forEach((item, index) => {
        if (item.fullPath === fullPath) {
          this.tagList.splice(index, 1);
          this.cacheTabList.delete(item.name);
        }
      });
    },
    addCache(name: string) {
      if (isString(name) && name !== '') this.cacheTabList.add(name);
    },
    deleteCache(tag: TagProps) {
      this.cacheTabList.delete(tag.name);
    },
    freshTabList(tags: TagProps[]) {
      this.tagList = tags;
      this.cacheTabList.clear();
      // 要先判断ignoreCache
      this.tagList
        .filter((el) => !el.ignoreCache)
        .map((el) => el.name)
        .forEach((x) => this.cacheTabList.add(x));
    },
    resetTabList() {
      this.tagList = [
        {
          fullPath: '/dashboard/workplace',
          ignoreCache: undefined,
          label: undefined,
          name: 'Workplace',
          portal: '/system',
          query: {},
          title: 'menu.dashboard.workplace',
        },
      ];
      this.cacheTabList.clear();
      this.cacheTabList.add(DEFAULT_ROUTE_NAME);
    },
  },
});

export default useTabBarStore;
